//
//  ExaminationRemindViewController.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-2-20.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "ExaminationRemindViewController.h"
#import "MainTabBarController.h"
#import "EXRChooseTypeViewController.h"
#import "EXRCell.h"
#import "LoginViewController.h"

#import "AppDelegate.h"

@interface ExaminationRemindViewController ()

@end

@implementation ExaminationRemindViewController


- (id)init{
    if (self = [super init]) {
        m_EXR_DataArray = [[NSMutableArray alloc] init];
    }
    return self;
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    MainTabBarController* tMainTabController = (MainTabBarController*)self.navigationController.tabBarController;

    if ( tMainTabController.m_UserDic != nil) {
        if ([m_titleBttBarItem superview] == nil) {
            [self.navigationController.navigationBar addSubview:m_titleBttBarItem];
        }
        if (m_AddBttBarItem != nil) {
            [self.navigationController.navigationBar addSubview:m_AddBttBarItem];
        }
        NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
        HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
        iHttpInterface.delegate = self;
        NSString* userid = [tUserDef objectForKey:USER_ID];
        [iHttpInterface GetExReRequestGet:userid];
        [self addHudLoad];
    }else{
        LoginViewController* tLogin = [[LoginViewController alloc] init];
        tLogin.pushtype = 0;
        tLogin.hidesBottomBarWhenPushed = YES;
        tLogin.navigationItem.hidesBackButton =YES;
        [self.navigationController pushViewController:tLogin animated:YES];
        [tLogin release];
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (m_AddBttBarItem.superview) {
        [m_AddBttBarItem removeFromSuperview];
    }
    if ([m_titleBttBarItem superview]) {
        [m_titleBttBarItem removeFromSuperview];
    }

}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        if ([self.navigationController.navigationBar viewWithTag:0x300] == nil) {
            UIImageView* tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titleBar.png"]];
            tImgView.tag =0x300;
            tImgView.frame = CGRectMake(0, 0, 320, 44);
            [self.navigationController.navigationBar addSubview:tImgView];
            [tImgView release];
        }
    }
    
    m_titleBttBarItem = [[MainTabBarController setTitle:@"考试提醒"] retain];
    m_type = EXR_NO_DATA;
    m_AddBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    m_AddBttBarItem.frame = CGRectMake(self.navigationController.navigationBar.frame.size.width-40,10, 24, 24);
    //[m_AddBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
    [m_AddBttBarItem setImage :[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
    [m_AddBttBarItem addTarget:self action:@selector(ChangeAdd) forControlEvents:UIControlEventTouchUpInside];
    [m_AddBttBarItem retain];
 
    m_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-44-49) style:UITableViewStylePlain];
    [m_TableView setBackgroundColor:[UIColor clearColor]];
    m_TableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    m_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView* iview = [[UIView alloc] initWithFrame:m_TableView.frame];
    [iview setBackgroundColor:[UIColor colorWithRed:242/250.f green:242/255.0f blue:242/255.0f alpha:1.0]];
    m_TableView.backgroundView = iview;
    [iview release];
    [m_TableView setDataSource:self];
    [m_TableView setDelegate:self];
    
    tipTextView = [[UIView alloc] initWithFrame:CGRectMake(0,15,320, 40)];
    [tipTextView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"recommend_empty.png"]]];
    



}

- (void)ChangeAdd
{
    EXRChooseTypeViewController* Exrct = [[EXRChooseTypeViewController alloc] init];
    Exrct.SelectEXRid = [[NSMutableArray alloc] init];
    for (NSDictionary* dic in  m_EXR_DataArray) {
        NSString* ids = (NSString*)[dic objectForKey:@"province_id"];
        [Exrct.SelectEXRid addObject:ids];
    }
    Exrct.hidesBottomBarWhenPushed = YES;
    Exrct.navigationItem.hidesBackButton =YES;
    [self.navigationController pushViewController:Exrct animated:YES];
    [Exrct release];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_EXR_DataArray count];
}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tCellstr = @"EXRCell";
    EXRCell* tCell = [tableView dequeueReusableCellWithIdentifier:tCellstr];
    if (tCell == nil) {
        tCell = [[[EXRCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellstr ] autorelease];
        tCell.accessoryType = UITableViewCellAccessoryNone;
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSUInteger row = indexPath.row;
    NSDictionary*  dic = [m_EXR_DataArray objectAtIndex:row];
    NSString* titel = (NSString*)[dic objectForKey:@"title"];
    NSString* num = (NSString*)[dic objectForKey:@"days"];//[NSNumber numberWithInteger:].integerValue;
    [tCell setData:titel number:num];
    return tCell;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    
//    UIView* tSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
//    [tSectionHeaderView setBackgroundColor:[UIColor clearColor]];
//    UILabel* tLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 20)];
//    [tLab setBackgroundColor:[UIColor clearColor]];
//    [tLab setFont:[UIFont boldSystemFontOfSize:15]];
//    [tLab setTextColor:[UIColor colorWithRed:125/250.f green:125/255.0f blue:125/255.0f alpha:1.0]];
//    [tLab setText:@"设置要提醒的考试"];
//    [tSectionHeaderView addSubview:tLab];
//    [tLab release];
//    return [tSectionHeaderView autorelease];
//}

#pragma mark UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 30;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
		[tAppDel.window addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsGetExRe:(NSDictionary*)dic
{
    if (dic!=nil) {
        NSArray* iArray = [dic objectForKey:@"list"];
        if ([iArray count]>0) {
            [m_EXR_DataArray removeAllObjects];
            for (NSObject* object in iArray) {
                [m_EXR_DataArray addObject:object];
            }
            if ([tipTextView superview]) {
                [tipTextView removeFromSuperview];
            }
            m_type = EXR_YES_DATA;
        }else{
            if ([m_TableView superview]) {
                [m_TableView removeFromSuperview];
            }
            m_type = EXR_NO_DATA;
        }
        
        if (m_type == EXR_NO_DATA) {
            [self.view addSubview:tipTextView];
        }else{
            [self.view addSubview:m_TableView];
            [m_TableView reloadData];
        }
    }
    [self hudWasHidden:nil];
}
-(void)RequestResultsErrer:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"error_msg"] isCue:YES delayTime:2.0f];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [m_AddBttBarItem release];
    [m_TableView release];
    [m_EXR_DataArray release];
    [tipTextView release];
    [m_titleBttBarItem release];
    [super dealloc];
}
@end
